package com.system.part.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.system.employee.dao.EmployeeRepository;
import com.system.employee.entity.Employee;
import com.system.part.dao.PartRepository;
import com.system.part.entity.Part;
import com.system.post.entity.Post;



@Service
@Transactional
public  class PartService implements IPartService {
	@Autowired	
	private PartRepository partRepository;
	
	public void saveOrUpdate(Part part) {
		
		partRepository.save(part);
	}
	public void delete(Part part) {
//		for(Post p:part.getPosts()) {
//		  p.setPart(null);
//		}
		partRepository.delete(part);
	}
	public void delete(List<Long> ids) {
		List<Part> parts =  (List<Part>) partRepository.findAll(ids);
		if(parts!=null) {
//			for(Part part:parts){
//				 for(Post p:part.getPosts()) {
//					 p.setPart(null);
//				 }
//				}
			partRepository.delete(parts);
		}
	}
	@Transactional(readOnly = true)
	public Part findOne(Long partId) {
		return partRepository.findOne(partId);
	}
	@Transactional(readOnly = true)
	public List<Part> findAll(){
		return (List<Part>) partRepository.findAll();
	}
	//动态条件查询
	@Transactional(readOnly = true)
	public Page<Part> findAll(Specification<Part> spec, Pageable pageable){
		return partRepository.findAll(spec, pageable);
	}

	public List<Employee> findEmployee(Long partId){
		return (List<Employee>)partRepository.findEmployee(partId);
		
	}
		
}
